#!/usr/bin/perl -w

######################################################
# file : perl/diarysearch.pl
# Author : Anil Kainikara (anilkumar911@gmail.com)
# do various things with diary. search, report etc...
#
# This code is licenced under the GPL.
#
######################################################

use Time::Local 'timelocal_nocheck';
#use Getopt::Std;
use Getopt::Long;
use DBI;
use strict;

my @weekday = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);
my @searchwords;
my @excludewords;

my @patterns;
my @tags;
my $showfullsearch=0;

my $db = DBI->connect("dbi:SQLite:/home/anil/.local/share/diary.db", "", "",
{RaiseError => 1, AutoCommit => 1});

sub main
{
   my $subcommand="";
   $subcommand=shift(@_);
   if ($subcommand eq "report") {
     report(); 
   } elsif ($subcommand eq "search" ){
     parsesearchargs(@_);
     search(@ARGV);
   }else {
      print "$subcommand is invalid \n";
      usage();
   }
}
   

sub usage()
{
   print "todo : @_"
}

sub report()
{
  my $sth = $db->prepare("SELECT day, month, year, content  FROM  entries ordr by year, month, day");

  my $rv = $sth->execute();
  my $day=0;
  my $month=0;
  my $year=0;
  my $content="";

  while ( ($day,$month,$year,$content) =  $sth->fetchrow_array() )
  {
    my $day_of_week = get_day($month."-".$day."-".$year);
    print "\n#########################################################\n";
    print "$day/$month/$year($day_of_week)\n";
    print "#########################################################\n";
    print "$content\n";
  }
}


sub parsesearchargs
{
  Getopt::Long::Configure ('bundling');
  GetOptions('e=s'=> \@patterns, 't=s'=> \@tags, 'f' => \$showfullsearch);
}

sub search()
{
  my $sth = $db->prepare("SELECT day, month, year, content  FROM  entries");

  my $rv = $sth->execute();
  my $day=0;
  my $month=0;
  my $year=0;
  my $content="";

  my $contentfound=0;
  my $searchpattern="";
  my $pattern="";

  my $searchtypetag=0;

  if( $searchtypetag != 1){
    for $pattern (@patterns){
    print "\n appending . . . $pattern\n";
      if($searchpattern ne ""){
        $searchpattern = $searchpattern . "|" . $pattern;
      } else {
        $searchpattern = $pattern;
      }
    }
  } else {
    my $tag;
    for $tag (@tags){
      if($searchpattern ne ""){
        $searchpattern = $searchpattern . "|" . "\<$tag\>.*\</$tag\>";
      } else {
        $searchpattern = "\<$tag\>.*\</$tag\>";
      }
    }
  }

  while ( ($day,$month,$year,$content) =  $sth->fetchrow_array() )
  {
    my $day_of_week = get_day($month."-".$day."-".$year);
    if( $showfullsearch == 1 ){
    print "\n searchpattern is  . . . $searchpattern\n";
      if( $content =~ m/($searchpattern)/si ){
        my $blockmatch = 
        print "\n#########################################################\n";
        print "$day/$month/$year($day_of_week)\n";
        print "#########################################################\n";
        #print "$content\n";
        print "$1\n";
      }
    } else {
      my $dateprinted=0;
      my $line="";
      while ( $content =~ m/(.*)/g )
      {
         $line=$1;
         if( $1 =~ m/$searchpattern/i ){
           if( $dateprinted != 1 ){
             print "\n#########################################################\n";
             print "$day/$month/$year($day_of_week)\n";
             print "#########################################################\n";
             $dateprinted = 1;
           }
           print "$line \n";
         }
       }
     }
  }
}


sub get_day {
my $date = shift || return(0);
my ($mon,$mday,$year) = $date =~ /(\d+)-(\d+)-(\d+)/;
my $epochtime = timelocal_nocheck(0, 0, 0, $mday, $mon-1, $year);
my $day = (localtime($epochtime))[6];
return $weekday[$day];
}

main(@ARGV);
